/* wowrxr.p - Work Order Route ROP                                            */
/* Copyright 1986-2011 QAD Inc., Santa Barbara, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Id:: wowrxr.p 2366 2010-11-10 01:47:19Z zzh                            $: */
/*V8:ConvertMode=NoConvert                                                    */
/* $Revision: 1.8 $                                                           */
/*                                                                            */
/*                                                                            */
/* Revision: 1.8.2.1  BY:hze           DATE:07/12/00  ECO: *N0DK*             */
/* REVISION: 9.1    LAST MODIFIED: 08/12/00 BY: *N0KC* myb                    */
/* REVISION: 9.1    LAST MODIFIED: 08/21/00 BY: *N0LJ* Mark Brown             */
/* Revision: 1.8.2.1 BY: Paul Donnelly (SB) DATE: 06/28/03 ECO: *Q00N* */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/
/*-Revision end---------------------------------------------------------------*/

/*                                                                            */
/*                                                                            */

/* ========================================================================== */
/* ******************************* DEFINITIONS ****************************** */
/* ========================================================================== */

/* STANDARD INCLUDE FILES */
{us/bbi/mfdeclre.i}
{us/px/pxmaint.i}
{us/px/pxphdef.i wowoxr}
{us/px/pxphdef.i ppicxr}
{us/wo/woconsdf.i}
{us/wr/wrttiro.i}
/* ========================================================================== */
/* ******************************* MAIN BLOCK ******************************* */
/* ========================================================================== */

/* ========================================================================== */
/* ******************************** FUNCTIONS ******************************* */
/* ========================================================================== */

/*============================================================================*/
FUNCTION isSubcontractOperationMissing returns logical
      (input pAvailableWr_route as logical,
      input pWOStatus as character):
/*------------------------------------------------------------------------------
Purpose:       determines if the subcontract operation is missing.
wowrxr.p
isSubcontractOperationMissing RETURNS logical (
input logical pAvaliableWr_route,
input character pWOStatus)

Parameters:
pWr_route -
pWOStatus -

Exceptions:

PreConditions:

PostConditions:

</Comment1>
<Comment2>
Notes:

</Comment2>
<Comment3>
History:

</Comment3>
------------------------------------------------------------------------------*/
   define variable returnData as logical no-undo.

   /* WO OPER DOES NOT EXIST, DO YOU WISH TO ADD */
   if not pAvailableWr_route and index ("FPC",pWOStatus) = 0 then
      returnData = yes.
   else
      returnData = no.

   return (returnData).
END FUNCTION.

/* ========================================================================== */
/* *************************** INTERNAL PROCEDURES ************************** */
/* ========================================================================== */

/*============================================================================*/
PROCEDURE createSubcontractOperation:
/*------------------------------------------------------------------------------
Purpose:       Create subcontract operation.
wowrxr.p
createSubcontractOperation (
Buffer pWo_mstr,
Buffer pWr_route,
input integer pWorkOrderOperation)

Parameters:
pWo_mstr -
pWr_route -
pWorkOrderOperation -

Exceptions:

PreConditions:

PostConditions:

</Comment1>
<Comment2>
Notes:

</Comment2>
<Comment3>
History:

</Comment3>
------------------------------------------------------------------------------*/
define  parameter buffer pWo_mstr for wo_mstr.
define  parameter buffer pWr_route for wr_route.
define input parameter pWorkOrderOperation as integer no-undo.

define variable workCenterSupplier like wc_vend no-undo.
define variable vProductionOrderOpenQty like wo_qty_ord no-undo.
define buffer ro_det for ro_det.

do on error undo, return error {&GENERAL-APP-EXCEPT}:

   {us/px/pxrun.i &PROC  ='getOpenQuantity'
      &PROGRAM = 'wowoxr.p'
      &HANDLE=ph_wowoxr
      &PARAM = "(buffer pwo_mstr,
        output vProductionOrderOpenQty)"
      }
   create pWr_route.
   pWr_route.wr_domain = global_domain.
   assign wr_nbr = wo_nbr
      wr_lot = wo_lot
      wr_op = pWorkOrderOperation
      wr_start = today
      wr_due = today
      wr_qty_ord = max(vProductionOrderOpenQty,0)
      wr_part = wo_part.

   for first ro_det

      where ro_det.ro_domain = global_domain and (  ro_routing = (if
      wo_routing <> "" then wo_routing
      else wo_part)
      and   ro_op      = pWorkOrderOperation
      and  (ro_start   = ? or ro_start <= today)
      and  (ro_end     = ? or ro_end   >= today)
      ) no-lock:
   end. /* FOR FIRST RO_DET */
   if available ro_det then do:
      if (ro_start = ? or ro_start <= wr_start)
         and (ro_end = ? or ro_end >= wr_start)
      then do:

         /* RETRIEVE SUPPLIER FROM ASSOCIATED WORK CENTER */
         {us/px/pxrun.i &PROC = 'getWorkCenterSupplier'
            &PROGRAM = 'rwroxr.p'
            &PARAM = "(input ro_wkctr,
              input ro_mch,
              output workCenterSupplier)"
            &NOAPPERROR = true
            &CATCHERROR = true}

         assign wr_std_op = ro_std_op
            wr_desc = ro_desc
            wr_wkctr = ro_wkctr
            wr_mch = ro_mch
            wr_setup = ro_setup
            wr_run = ro_run
            wr_move = ro_move
            wr_tool = ro_tool

            wr_vend = workCenterSupplier.

      end. /*if ro_start*/
   end. /*if avail ro_det*/
end.
return {&SUCCESS-RESULT}.
END PROCEDURE.

/*============================================================================*/
PROCEDURE readOperationForFirstWOID:
/*------------------------------------------------------------------------------
Reads the first operation for a work order.
wowrxr.p
readOperationForFirstWOID (
input character pWorkOrderId,
input integer pWorkOrderOperation,
Buffer pWr_route,
input logical pLockRecord,
input logical pWait)

Parameters:
pWorkOrderId -
pWorkOrderOperation -
pWr_route -
pLockRecord -
pWait -

Exceptions:

PreConditions:

PostConditions:

</Comment1>
<Comment2>
Notes:

</Comment2>
<Comment3>
History:

</Comment3>
------------------------------------------------------------------------------*/
define input parameter pWorkOrderId as character no-undo.
define input parameter pWorkOrderOperation as integer no-undo.
define  parameter buffer pWr_route for wr_route.
define input parameter pLockRecord as logical no-undo.
define input parameter pWait as logical no-undo.

do on error undo, return error {&GENERAL-APP-EXCEPT}:

   if pLockRecord then do:
      if pWait then do:
         find first pWr_route exclusive-lock
            where pWr_route.wr_domain = global_domain and
            wr_lot = pWorkOrderId and
            wr_op  = pWorkOrderOperation no-error.
      end. /* if pWait */
      else do:
         find first pWr_route exclusive-lock
            where pWr_route.wr_domain = global_domain and
            wr_lot = pWorkOrderId and
            wr_op  = pWorkOrderOperation no-error no-wait.
         if locked pWr_route then
            return {&RECORD-LOCKED} .
      end. /* else */
   end. /* if pLockRecord */
   else do:
      find first pWr_route no-lock
         where pWr_route.wr_domain = global_domain and
         wr_lot = pWorkOrderId and
         wr_op  = pWorkOrderOperation no-error.
   end. /* else */

   if not available pWr_route then
      return {&RECORD-NOT-FOUND}.

end.
return {&SUCCESS-RESULT}.
END PROCEDURE.

/* ========================================================================== */
/* *************************** INTERNAL PROCEDURES ************************** */
/* ========================================================================== */

/*============================================================================*/
PROCEDURE readNextOperationForWOID:
/*------------------------------------------------------------------------------
Reads the next operation for a work order.
wowrxr.p
readNextOperationForWOID (
input character pWorkOrderLot,
input integer pWorkOrderOperation,
Buffer pWr_route,
input logical pLockRecord,
input logical pWait)

Parameters:
pWorkOrderLot -
pWorkOrderOperation -
pWr_route -
pLockRecord -
pWait -

Exceptions:

PreConditions:

PostConditions:

</Comment1>
<Comment2>
Notes:

</Comment2>
<Comment3>
History:

</Comment3>
------------------------------------------------------------------------------*/
define input parameter pWorkOrderLot as character no-undo.
define input parameter pWorkOrderOperation as integer no-undo.
define  parameter buffer pWr_route for wr_route.
define input parameter pLockFlag as logical no-undo.
define input parameter pWaitFlag as logical no-undo.

do on error undo, return error {&GENERAL-APP-EXCEPT}:

   if plockFlag then do:
      if pWaitFlag then
      for first pWr_route  where pWr_route.wr_domain = global_domain and
         wr_lot = pWorkOrderLot and
         wr_op > pWorkOrderOperation
      exclusive-lock:
      end.
      else do:
         find first pWr_route exclusive-lock
            where pWr_route.wr_domain = global_domain and
            wr_lot = pWorkOrderLot and
            wr_op > pWorkOrderOperation
            no-error no-wait.
         if locked pWr_route then
            return {&RECORD-LOCKED}.
      end.
   end.
   else
   for first pWr_route  where pWr_route.wr_domain = global_domain and
      wr_lot = pWorkOrderLot and
      wr_op > pWorkOrderOperation
   no-lock:
   end.

   if not available pWr_route then
      return {&RECORD-NOT-FOUND}.

end.
return {&SUCCESS-RESULT}.
END PROCEDURE.

/*============================================================================*/
PROCEDURE validateSubcontractCost:
/*------------------------------------------------------------------------------
Validates a sub contract cost
wowrxr.p
validateSubcontractCost (
input decimal pSubcontractCost)

Parameters:
pSubcontractCost -

Exceptions:

PreConditions:

PostConditions:

</Comment1>
<Comment2>
Notes:

</Comment2>
<Comment3>
History:

</Comment3>
------------------------------------------------------------------------------*/
define input parameter pSubcontractCost as decimal no-undo.

do on error undo, return error {&GENERAL-APP-EXCEPT}:
   if pSubcontractCost = 0 then do:
      /* MESSAGE #5118 - THERE IS NO SUBCONTRACT COST FOR THIS OPERATION */
      {us/bbi/pxmsg.i &MSGNUM=5118
         &ERRORLEVEL={&WARNING-RESULT}}
      return {&WARNING-RESULT}.
   end.
end.
return {&SUCCESS-RESULT}.
END PROCEDURE.

/*============================================================================*/
PROCEDURE initialize_tt_iro_det:
/*------------------------------------------------------------------------------
Procedure empties the temp table tt_iro_det:

Parameters:

Exceptions:

PreConditions:

PostConditions:

Notes:

------------------------------------------------------------------------------*/

define buffer tt_iro_det for tt_iro_det.

do on error undo, return error {&GENERAL-APP-EXCEPT}:

   empty temp-table tt_iro_det no-error.

end.
return {&SUCCESS-RESULT}.
END PROCEDURE. /* initialize_tt_iro_det */

/*============================================================================*/
PROCEDURE getCumUnitCostToOp:
/*------------------------------------------------------------------------------
Returns the total cumulative cost at an operation of a WO routing,
i.e. the total cost for all prior operations including the operation
passed in as a parameter.
Parameters:
input p_wo_lot - The workorder ID for the production order
input p_wr_op  - The operation at which you want the cost
input p_costSource - Where the cost should come from, can be GLCost, i.e.
the cost from the item routing as rolled up in a regular cost roll up
Or it can be the cost calculated for the routing and BOM on
this production order
output p_cumCostToOp - The total cost for all operations up to and including
the operation passed in.

Exceptions:

PreConditions:

PostConditions:

Notes:

------------------------------------------------------------------------------*/
define input  parameter p_wo_lot      like wo_lot        no-undo.
define input  parameter p_wr_op       like wr_op         no-undo.
define input  parameter p_costSource  as character       no-undo.
define output parameter p_cumCostToOp like sct_cst_tot   no-undo.

define buffer tt_iro_det for tt_iro_det.

do on error undo, return error {&GENERAL-APP-EXCEPT}:
   run getRoutingCosts (input p_wo_lot,
      input p_costSource,
      output table tt_iro_det ).

   if return-value <> {&SUCCESS-RESULT}
      then return return-value.

   find tt_iro_det where tt_iro_costSource = p_costSource and
      tt_iro_lot = p_wo_lot and
      tt_iro_op = p_wr_op
   no-lock no-error.

   if available tt_iro_det
      then p_cumCostToOp = tt_iro_cost_tot.
   else p_cumCostToOp = 0.

end.
return {&SUCCESS-RESULT}.
END PROCEDURE. /* getCumUnitCostToOp */

/*============================================================================*/
PROCEDURE getRoutingCosts:
/*------------------------------------------------------------------------------
Retrieves and returns one record per each operation on
the Production Order Routing.

Parameters:
input p_wo_lot - The workorder ID for the production order to roll up
input p_costSource - Where the cost should come from, can be GLCost, i.e.
the cost from the item routing as rolled up in a regular cost roll up
Or it can be the cost calculated for the routing and BOM on
this production order
output tt_iro_det - temp table similar to iro_det with the rolled up cost.

Exceptions:

PreConditions:

PostConditions:

Notes:

------------------------------------------------------------------------------*/
define input parameter p_wo_lot  like wo_mstr.wo_lot      no-undo.
define input parameter p_costSource  as character         no-undo.
define output parameter table    for  tt_iro_det          bind.

define buffer wo_mstr for wo_mstr.

do on error undo, return error {&GENERAL-APP-EXCEPT}:

   /* Don't need to do anything if the records already exist */
   if can-find (first tt_iro_det where tt_iro_costSource = p_costSource and
      tt_iro_lot = p_wo_lot)
      then return {&SUCCESS-RESULT}.

   /* Make sure the wo_lot is valid, else return out */
   find wo_mstr where wo_domain = global_domain and
      wo_lot = p_wo_lot
   no-lock no-error.
   if not available wo_mstr
      then return {&RECORD-NOT-FOUND}.

   if p_costSource = {&IRO_COST_SOURCE_WO_STD}
   then do:
      run getWORoutingCosts (input p_wo_lot,
         output table tt_iro_det ).

      if return-value <> {&SUCCESS-RESULT}
         then return return-value.
   end.
   if p_costSource = {&IRO_COST_SOURCE_GL_STD}
   then do:
      run getStdRoutingCosts   (input p_wo_lot,
         output table tt_iro_det ).

      if return-value <> {&SUCCESS-RESULT}
         then return return-value.
   end.
end.
return {&SUCCESS-RESULT}.

END PROCEDURE. /* getRoutingCosts */

/*============================================================================*/
PROCEDURE getStdRoutingCosts private:
/*------------------------------------------------------------------------------
Returns one record per each operation on the Production Order Routing,
the records are copied from iro_det. It calculates the cost for each
operation on the routing of the order and updates iro_det if the iro_det
doesn't exist
Parameters:
input p_wo_lot - The workorder ID for the production order to roll up
output tt_iro_det - temp table similar to iro_det with the rolled up cost.

Exceptions:

PreConditions:

PostConditions:

Notes:

------------------------------------------------------------------------------*/
define input parameter p_wo_lot  like wo_mstr.wo_lot      no-undo.
define output parameter table    for  tt_iro_det.

define buffer wo_mstr for wo_mstr.
define buffer iro_det for iro_det.
define buffer pt_mstr for pt_mstr.
define buffer ptp_det for ptp_det.

define variable cur_cost_mthd as character no-undo.
define variable cur_cost_set  as character no-undo.
define variable gl_cost_mthd  as character no-undo.
define variable gl_cost_set   as character no-undo.
define variable routing       as character no-undo.
define variable bom_code      as character no-undo.
define variable stdOrderQty   as decimal   no-undo.

do on error undo, return error {&GENERAL-APP-EXCEPT}:

   find wo_mstr where wo_domain = global_domain and
      wo_lot = p_wo_lot
   no-lock no-error.
   if not available wo_mstr
      then return {&RECORD-NOT-FOUND}.

   routing = wo_part.
   bom_code = wo_part.

   if wo_routing > ""
      then routing = wo_routing.

   if wo_bom_code > ""
      then bom_code = wo_bom_code.

   {us/bbi/gprun.i ""csavg01.p"" "(input wo_part,
        input wo_site,
        output gl_cost_set,
        output gl_cost_mthd,
        output cur_cost_set,
        output cur_cost_mthd)"}

   if not can-find (first iro_det
      where iro_domain   = global_domain
      and   iro_part     = bom_code
      and   iro_site     = wo_site
      and   iro_cost_set = gl_cost_set
      and   iro_routing  = routing)
   then do:
      stdOrderQty = 1.

      find pt_mstr
         where pt_domain = global_domain
         and   pt_part   = wo_part
      no-lock no-error.

      if pt_ord_qty <> 0
         then stdOrderQty = pt_ord_qty.

      find ptp_det
         where ptp_domain = global_domain
         and   ptp_part   = wo_part
         and   ptp_site   = wo_site
      no-lock no-error.

      if available ptp_det
      then do:
         if ptp_ord_qty <> 0
            then
            stdOrderQty = ptp_ord_qty.
      end. /* IF AVAILABLE ptp_det */

      /* Build iro_det */
      {us/bbi/gprun.i ""rwroru1a.p"" "(wo_site,
           gl_cost_set,
           routing,
           bom_code,
           stdOrderQty,
           today,
           yes)"}
   end. /* if not can-find (first iro_det */

   for each iro_det
         where iro_domain   = global_domain
         and   iro_part     = bom_code
         and   iro_site     = wo_site
         and   iro_cost_set = gl_cost_set
         and   iro_routing  = routing
      no-lock.

      create tt_iro_det.
      assign
         tt_iro_costSource = {&IRO_COST_SOURCE_GL_STD}
         tt_iro_lot        = wo_lot
         tt_iro_op         = iro_op
         tt_iro_cost_tot   = iro_cost_tot
         tt_iro_mtl_ll     = iro_mtl_ll
         tt_iro_mtl_tl     = iro_mtl_tl
         tt_iro_lbr_ll     = iro_lbr_ll
         tt_iro_lbr_tl     = iro_lbr_tl
         tt_iro_bdn_ll     = iro_bdn_ll
         tt_iro_bdn_tl     = iro_bdn_tl
         tt_iro_sub_ll     = iro_sub_ll
         tt_iro_sub_tl     = iro_sub_tl
         tt_iro_ovh_ll     = iro_ovh_ll
         tt_iro_ovh_tl     = iro_ovh_tl
         .

   end. /* for each iro_det */
end.
return {&SUCCESS-RESULT}.

END PROCEDURE. /* getStdRoutingCosts */

/*============================================================================*/
PROCEDURE getWORoutingCosts private:
/*------------------------------------------------------------------------------
Calculates the cost for each operation on a Production Order.
Returns one record per each operation on the Production Order Routing.
Parameters:
input p_wo_lot - The workorder ID for the production order to roll up
output tt_iro_det - temp table similar to iro_det with the rolled up cost.

Exceptions:

PreConditions:

PostConditions:

Notes:

------------------------------------------------------------------------------*/
define input parameter p_wo_lot  like wo_mstr.wo_lot      no-undo.
define output parameter table    for  tt_iro_det.

define variable cumYield         like wr_yield_pct        no-undo.
define variable opYield          like wr_yield_pct        no-undo.
define variable itemIsPhantom    like pt_phantom          no-undo.
define variable itemStandardCost like sct_cst_tot         no-undo.
define variable orderQuantity    like wo_qty_ord          no-undo.
define variable numberOfSetups   as integer initial 1     no-undo.
define variable dummy-decimal    as decimal               no-undo.

define buffer wo_mstr               for wo_mstr.
define buffer DiscreteSchedwo_mstr  for wo_mstr.
define buffer wod_det               for wod_det.
define buffer wr_route              for wr_route.
define buffer tt_iro_det            for tt_iro_det.
define buffer PrevOp_tt_iro_det     for tt_iro_det.
define buffer pt_mstr               for pt_mstr.
define buffer ptp_det               for ptp_det.

do on error undo, return error {&GENERAL-APP-EXCEPT}:

   find wo_mstr where wo_mstr.wo_domain = global_domain and
      wo_mstr.wo_lot = p_wo_lot
   no-lock no-error.

   if not available wo_mstr
      then return {&RECORD-NOT-FOUND}.
   cumYield = 1.

   /* Get the order quantity to use in the calculations. For WO use the
   wo_qty_ord, for repetitive, since there is no order qty on cum order
   use pt/ptp_ord_qty. */
   if wo_mfg_type = {&MFG_TYPE_DISCRETE}
      then
   if wo_type = {&WO_TYPE_CUMULATIVE}
   then do:
      {us/px/pxgetph.i wowoxr}
      orderQuantity = {us/px/pxfunct.i &FUNCTION='getTotalOrderQuantity'
         &PROGRAM='wowoxr.p'
         &HANDLE=ph_wowoxr
         &PARAM="input wo_mstr.wo_domain,
           input wo_mstr.wo_lot" }.
      numberOfSetups = 0.
      for each DiscreteSchedwo_mstr
            where DiscreteSchedwo_mstr.wo_domain = wo_mstr.wo_domain and
            DiscreteSchedwo_mstr.wo_cum_lot_id = wo_mstr.wo_lot and
            DiscreteSchedwo_mstr.wo_lot <> wo_mstr.wo_lot
         no-lock:
         numberOfSetups = numberOfSetups + 1.
      end.

   end.
   else orderQuantity = wo_qty_ord.
   else do:
      find ptp_det where ptp_domain = global_domain and
         ptp_part = wo_part and
         ptp_site = wo_site
      no-lock no-error.
      if available ptp_det
         then orderQuantity = ptp_ord_qty.
      else do:
         find pt_mstr where pt_domain = global_domain and
            pt_part = wo_part
         no-lock no-error.
         if available pt_mstr
            then orderQuantity = pt_ord_qty.
         else orderQuantity = 1.
      end.
   end.
   if orderQuantity = 0 then orderQuantity = 1.

   /* Read each operation on the order and calculate its op costs */
   for each wr_route where wr_domain = global_domain and
         wr_lot = wo_lot
      no-lock
         break by wr_op:

      find wc_mstr where wc_domain = global_domain and
         wc_wkctr  = wr_wkctr and
         wc_mch    = wr_mch
      no-lock no-error.

      opYield = wr_yield_pct * 0.01.

      create tt_iro_det.
      assign tt_iro_costSource = {&IRO_COST_SOURCE_WO_STD}
         tt_iro_lot = wo_lot
         tt_iro_op  = wr_op.

      /* Move cost from prior op into lower level (ll) fields of this op. */
      for last PrevOp_tt_iro_det
         where PrevOp_tt_iro_det.tt_iro_lot = p_wo_lot and
         PrevOp_tt_iro_det.tt_iro_op < tt_iro_det.tt_iro_op
      no-lock:

      if available PrevOp_tt_iro_det
      then do:
         /* The cost of the lower level operations have to be inflated by
         the yield of this operation to get the accurate total cost at
         this operation. */
         assign
            tt_iro_det.tt_iro_mtl_ll = (PrevOp_tt_iro_det.tt_iro_mtl_tl +
            PrevOp_tt_iro_det.tt_iro_mtl_ll
            ) / opYield
            tt_iro_det.tt_iro_lbr_ll = (PrevOp_tt_iro_det.tt_iro_lbr_tl +
            PrevOp_tt_iro_det.tt_iro_lbr_ll
            ) / opYield
            tt_iro_det.tt_iro_bdn_ll = (PrevOp_tt_iro_det.tt_iro_bdn_tl +
            PrevOp_tt_iro_det.tt_iro_bdn_ll
            ) / opYield
            tt_iro_det.tt_iro_sub_ll = (PrevOp_tt_iro_det.tt_iro_sub_tl +
            PrevOp_tt_iro_det.tt_iro_sub_ll
            ) / opYield
            tt_iro_det.tt_iro_ovh_ll = (PrevOp_tt_iro_det.tt_iro_ovh_tl +
            PrevOp_tt_iro_det.tt_iro_ovh_ll
            ) / opYield
            .
         end. /* if available PrevOp_tt_iro_det */
      end. /* for last PrevOp_tt_iro_det */

      /* Get cost of items that are going in to the WO before the first op. */
      if first (wr_op) then
      for each wod_det where wod_domain = global_domain and
            wod_lot = p_wo_lot and
            wod_op = 0
         no-lock:

         /* Bypass phantom items, their components will be used for
         the cost roll-up */
         {us/bbi/gprun.i ""gpckphan.p""
            "(input wod_det.wod_part,
              input wod_det.wod_site,
              output itemIsPhantom)"}
         if itemIsPhantom then next.

         {us/bbi/gprun.i ""gpsct05.p""
            "(input wod_part,
              input wo_site,
              input 1,                    /* Get Total Cost */
              output itemStandardCost,
              output dummy-decimal)"
            }

         tt_iro_mtl_ll = tt_iro_mtl_ll + wod_bom_qty * itemStandardCost / opYield.

      end. /* for each wod_det where wod_op = "" */

      /* Get cost of items that are going in to this op. */
      for each wod_det where wod_domain = global_domain and
            wod_lot = p_wo_lot and
            wod_op = wr_op
         no-lock:
         /* Bypass phantom items, their components will be used for
         the cost roll-up */
         {us/bbi/gprun.i ""gpckphan.p""
            "(input wod_det.wod_part,
              input wod_det.wod_site,
              output itemIsPhantom)"}
         if itemIsPhantom then next.

         {us/bbi/gprun.i ""gpsct05.p""
            "(input wod_part,
              input wo_site,
              input 1,                    /* Get Total Cost */
              output itemStandardCost,
              output dummy-decimal)"
            }

         tt_iro_mtl_tl = tt_iro_mtl_tl + wod_bom_qty * itemStandardCost / opYield.

      end. /* for each wod_det where wod_op = wr_op */

      /* Multiply the seetup-time by the number of setups, number of
      setups can be > 1 if the calculation is done for a CUM Discrete
      order, where we would add up all scheduled orders to find the
      number of setups to used */
      tt_iro_lbr_tl = (wr_setup * numberOfSetups * wc_setup_rte
      / (orderQuantity * cumYield)
      + wr_run * wc_lbr_rate
      ) / opYield.

      {us/bbi/gprun.i ""rwbdncal.p""
         "( input 'TOTAL'              /* BURDEN COST TYPE */,
           input no                  /* ACCUMULATE TOTAL? */,
           input (wc_bdn_pc * 0.01)     /* BURDEN PERCENT */,
           input wc_bdn_rate               /* BURDEN RATE */,
           input wc_lbr_rate                /* LABOR RATE */,
           input wc_mch_bdn        /* MACHINE BURDEN RATE */,
           input wr_mch_op               /* MACHINES USED */,
           input orderQuantity * cumYield * opYield /* ORDER QUANTITY */,
           input wr_run                  /* UNIT RUN TIME */,
           input wc_setup_rte               /* SETUP RATE */,
           input wr_setup * numberOfSetups  /* SETUP TIME */,
           input opYield                 /* YIELD PERCENT */,
           input-output tt_iro_bdn_tl      /* BURDEN COST */
           )"
         }

      assign
         /* Do not include yield in the subcontract cost,
         one only pays for the good items coming back from subcontractor */
         tt_iro_sub_tl = wr_sub_cost
         tt_iro_cost_tot =  tt_iro_mtl_tl  + tt_iro_mtl_ll
         + tt_iro_lbr_tl  + tt_iro_lbr_ll
         + tt_iro_bdn_tl  + tt_iro_bdn_ll
         + tt_iro_sub_tl  + tt_iro_sub_ll
         + tt_iro_ovh_tl  + tt_iro_ovh_ll
         cumYield = cumYield * opYield.

   end. /*    for each wr_route where wr_domain = global_domain and */

end. /* do on error undo, return error {&GENERAL-APP-EXCEPT}: */
return {&SUCCESS-RESULT}.
END PROCEDURE. /* rollUpWORoutingCosts */

/* extract from rechkq.p */
/* ========================================================================== */
/* ******************************** FUNCTIONS ******************************* */
/* ========================================================================== */

/*============================================================================*/
FUNCTION isFirstOperationForWOID returns logical
      (input pWorkOrderId as character,
      input pWorkOrderOperation as integer):
/*------------------------------------------------------------------------------
Checks if this is the first operation for a Work Order
wowrxr.p
isFirstOperationForWOID RETURNS logical (
input character pWorkOrderId,
input integer pWorkOrderOperation)

Parameters:
pWorkOrderId -
pWorkOrderOperation -

Exceptions:

PreConditions:

PostConditions:

</Comment1>
<Comment2>
Notes:

</Comment2>
<Comment3>
History:

</Comment3>
------------------------------------------------------------------------------*/
   define variable returnData as logical no-undo.

   if can-find(last wr_route  where wr_route.wr_domain = global_domain and
      wr_lot = pWorkOrderId and
      wr_op  < pWorkOrderOperation) then
      returnData = false.
   else
      returnData = true.

   return (returnData).
END FUNCTION.

